package cn.lzj.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TimeUtils {

    //字符串转换为日期
    public static Date StringContactToDate(String str){
        //yyyy-MM-dd HH:mm:ss
        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //1.string->date
        Date date ;
        try {
            date = dateFormat.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }

    //字符串转化为时间戳
    public static Long StringContactToTimeMillis(String str){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        long TimeMillis = 0L;
        try {
            date = sdf.parse(str);
            TimeMillis = date.getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return TimeMillis;
    }


    //时间戳转化为日期字符串
    public static String TimeMillisContactToString(Long TimeMillis){
        Date date = new Date(TimeMillis);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }


    //时间戳转化为日期
    public static Date TimeMillisContactToDate(Long TimeMillis){
        return new Date(TimeMillis);
    }



    //加一天
    public static String addOneDate(String nowDate){

        Long TimeMillis = StringContactToTimeMillis(nowDate);
        TimeMillis += 86400000;
        Date newDate = new Date(TimeMillis);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(newDate);
    }

    public static Date getFormatDate(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(date);
        TimeUtils.StringContactToDate(format);
        return date;
    }

}
